#include "global.h"

bool enqueue(Process* p, ProcessQueue* queue)
{
	if (queue)
	{
		Process* i = 0, *j = 0;
		for (i = queue->top; ((i != 0) && (i->priority >= p->priority)); i = i->next)
		{
			j = i;
		}
		
		if (!j)
		{
			p->next = queue->top;
			queue->top = p;
		}
		else
		{
			j->next = p;
			p->next = i;
		}
		++queue->size;
		return true;
	}
	else
	{
		printf("Tried to enqueue to invalid queue!\n");
		return false;
	}
}

Process* dequeue(ProcessQueue* queue)
{
	Process* p = 0;
	if (queue)
	{
		if (queue->size > 0)
		{
			p = queue->top;
			queue->top = p->next;
			p->next = 0;
			--queue->size;
		}
		else
		{
			printf("Tried to dequeue from an empty queue!\n");
		}
	}
	else
	{
		printf("Tried to dequeue from invalid queue!\n");
	}
	return p;
}

bool printQueue(ProcessQueue* queue)
{
	if (queue)
	{
		Process* i = 0;
		for (i = queue->top; i != 0; i = i->next)
		{
			printf("P%d => ", i->processID);
		}
		printf("NULL\n");
		return true;
	}
	return false;
}
